package com.lee.mao.database.utils;

import com.lee.mao.base.Mapplication;
import com.lee.mao.bean.ClassifyTable;
import com.lee.mao.database.dao.ClassifyTableDao;

import java.util.List;

public class ClassifyTableUtil {
    private static ClassifyTableUtil instance;


    private static Mapplication mapplication;

    public static ClassifyTableUtil getInstance(Mapplication app) {

        mapplication = app;
        if (instance == null) {
            synchronized (ClassifyTableUtil.class) {
                if (instance == null) {
                    instance = new ClassifyTableUtil();
                }
            }
        }
        return instance;
    }

    private ClassifyTableUtil() {
    }


    private ClassifyTableDao getTableDao() {

        if (mapplication != null)
            return mapplication.getDaoSession().getClassifyTableDao();
        else
            return null;
    }

    public List<ClassifyTable> getAllClassify() {

        ClassifyTableDao tableDao = getTableDao();

        if (tableDao != null)
            return tableDao.loadAll();
        else
            return null;

    }


    public void deleteClassify(long id) throws Exception {

        ClassifyTableDao tableDao = getTableDao();

        if (tableDao != null)
            tableDao.deleteByKey(id);
        else
            throw new Exception("删除失败， tableDao为null");

    }


    public void updateClassify(ClassifyTable classifyTable) throws Exception {
        ClassifyTableDao tableDao = getTableDao();
        if (tableDao != null)
            tableDao.update(classifyTable);
        else
            throw new Exception("更新失败， tableDao为null");
    }

    public void addClassify(ClassifyTable classifyTable) throws Exception {

        ClassifyTableDao tableDao = getTableDao();

        if (tableDao != null)
            tableDao.insert(classifyTable);
        else
            throw new Exception("增加失败， tableDao为null");
    }

}